﻿using System;
using System.Collections;
using Coolite.Ext.Web;
using Pap.Web.Modules.Reports.ViewModel;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Reports.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class ControlTheMovementOfTheUsersInTheSytemReport : BasePage<ControlTheMovementOfTheUsersInTheSytemReportViewModel>
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Master.GenerateReport += Master_GenerateReport;
            if (!IsPostBack && !Ext.IsAjaxRequest)
            {
                FillReportParams();
            }
        }

        protected void Master_GenerateReport(object sender, EventArgs e)
        {
            DateTime fromDate = Convert.ToDateTime(dtDateFrom.Value);
            DateTime toDate = Convert.ToDateTime(dtDateto.Value);
            var committeId = cmbCommittee.SelectedItem.Value.ToGuid();
            var userId = cmbUsers.SelectedItem.Value.ToGuid();
            int? loanNumber = null;
            if (!string.IsNullOrEmpty(numbLoanNumber.Text))
            {
                loanNumber = Convert.ToInt32(numbLoanNumber.Text);
            }


            this.strReport.DataSource = this.ViewModel.ControltheMovementofUsersoftheSystem(
                committeId, userId, loanNumber, fromDate, toDate);
            this.strReport.DataBind();
        }

        /// <summary>
        /// Handles the GetReport event of the Master control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        //void Master_GetReport(object sender, EventArgs e)
        //{
        //    DateTime fromDate = Convert.ToDateTime(dtDateFrom.Value);
        //    DateTime toDate = Convert.ToDateTime(dtDateto.Value);
        //    int? LoanNumber = null;
        //    if (!string.IsNullOrEmpty(numbLoanNumber.Text))
        //    {
        //        LoanNumber = Convert.ToInt32(numbLoanNumber.Text);
        //    }
        //    IEnumerable result = this.ViewModel.ControltheMovementofUsersoftheSystem(cmbCommittee.SelectedItem.Value.ToGuid(), cmbUsers.SelectedItem.Value.ToGuid(), LoanNumber, fromDate, toDate);


        //    ReportDataSource datasource = new ReportDataSource("dsUsersTransactions",
        //          result);

        //    ReportViewer1.LocalReport.DataSources.Clear();
        //    ReportViewer1.LocalReport.DataSources.Add(datasource);
        //    ReportViewer1.LocalReport.Refresh();
            
            
        //}

        /// <summary>
        /// Fills the report params.
        /// </summary>
        private void FillReportParams()
        {
            strUsers.DataSource = this.ViewModel.GetAllResponsibleUsers();
            strUsers.DataBind();

            strCommittee.DataSource = this.ViewModel.GetAllCommittee();
            strCommittee.DataBind();
        }
    }
}
